package com.shungen.apicloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shungen.apicloud.common.ErrorCode;
import com.shungen.apicloud.constant.CommonConstant;
import com.shungen.apicloud.exception.BusinessException;
import com.shungen.apicloud.mapper.InterfaceInfoMapper;
import com.shungen.apicloud.model.dto.interfaceinfo.InterfaceInfoQueryRequest;
import com.shungen.apicloud.model.entity.InterfaceInfo;
import com.shungen.apicloud.service.InterfaceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 接口信息(InterfaceInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-07-23 09:32:02
 */
@Service("interfaceInfoService")
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo> implements InterfaceInfoService {

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
    }

    @Override
    public Page<InterfaceInfo> selectPage(InterfaceInfoQueryRequest interfaceInfoQueryRequest, int pageSize, int current) {
        LambdaQueryWrapper<InterfaceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(interfaceInfoQueryRequest.getStatus() != null,InterfaceInfo::getStatus,interfaceInfoQueryRequest.getStatus())
                .eq(interfaceInfoQueryRequest.getMethod() != null,InterfaceInfo::getMethod,interfaceInfoQueryRequest.getMethod())
                .eq(interfaceInfoQueryRequest.getUserId() != null,InterfaceInfo::getUserId,interfaceInfoQueryRequest.getUserId())
                .like(interfaceInfoQueryRequest.getName() != null,InterfaceInfo::getName,interfaceInfoQueryRequest.getName())
                .like(interfaceInfoQueryRequest.getUrl() != null,InterfaceInfo::getUrl,interfaceInfoQueryRequest.getUrl())
                .like(interfaceInfoQueryRequest.getDescription() != null,InterfaceInfo::getDescription,interfaceInfoQueryRequest.getDescription());
        //处理排序
        if(interfaceInfoQueryRequest.getSortField() != null){
            boolean isDesc  = !CommonConstant.SORT_ORDER_ASC.equals(interfaceInfoQueryRequest.getSortOrder());//是否升序排序
            switch (interfaceInfoQueryRequest.getSortField()) {
                case "visitedCount":
                    if(isDesc){
                        queryWrapper.orderByDesc(InterfaceInfo::getVisitedCount);
                    }else{
                        queryWrapper.orderByAsc(InterfaceInfo::getVisitedCount);
                    }
                    break;
                case "createTime":
                    if(isDesc){
                        queryWrapper.orderByDesc(InterfaceInfo::getCreateTime);
                    }else{
                        queryWrapper.orderByAsc(InterfaceInfo::getCreateTime);
                    }
                    break;
                // 添加其他排序字段的情况
                default:
                    throw new BusinessException(ErrorCode.PARAMS_ERROR,"不合法的排序字段:" + interfaceInfoQueryRequest.getSortField());
            }
        }


        Page<InterfaceInfo> interfaceInfoPage = this.baseMapper.selectPage(new Page<InterfaceInfo>(current,pageSize), queryWrapper);
        return interfaceInfoPage;

    }
}

